<?php

class CComment extends Module
{
    function __construct()
    {
    }    
    private static $_obj = null;
    static function getInstance()
    {
        if( !self::$_obj )
        {
            self::$_obj = new self();
        }

        return self::$_obj;
    }    

    function display()
    {
        $url = isset( $_GET['url'] ) ? trim( $_GET['url'] ) : '';
        if( $url == '' )
        {
            parent::smartyOut( 'ccomment' );
            return;
        }
        
        $type = isset( $_GET['type'] ) ? trim( $_GET['type'] ) : 'new';
        $page = isset( $_GET['page'] ) ? trim( $_GET['page'] ) : 1;
        if( $type == 'hot' ) 
        {
            $tourl = "http://tcomment.qq.com/wb/essrtex/fetchAction?l=1&url={$url}&cn=20";
        }
        else
        {
            $tourl = "http://tcomment.qq.com/wb/rt/fetchAction?type=latest&url={$url}&page={$page}&pagesize=10&id=&time=";
        }

        $str = file_get_contents( $tourl );
        if( $str == '' )
        {
            throw new Exception( "打开评论列表失败" );
        }
        $arr = json_decode( $str, true );
        if( $arr == false )
        {
            throw new Exception( "解析json失败" );
        }
        if( isset( $arr['contents'] ) && $arr['contents'] ) foreach( $arr['contents'] as &$one )
        {
            $one['id'] = number_format( $one['id'], 0, '', '' );
            $one['userInfo']['nick'] = htmlentities ( $one['userInfo']['nick'], ENT_QUOTES, 'UTF-8' );
            $one['content'] = htmlentities ( $one['content'], ENT_QUOTES, 'UTF-8' );
        }
        if( $arr['contents'] ) $arr['bnext'] = 1;
        $arr['url'] = $url;
        $arr['type'] = $type;
        $arr['tourl'] = $tourl;
        parent::smartyOut( 'ccomment', $arr );
    }
}
























